import { Channel, Token } from '@/types/data'

const TOKEN_KEY = 'geek-h5-sh88'
const GEEK_CHANNEL = 'geek-channel'
const GEEK_HISTORY = 'geek_history'
// 存储token
export function setToken(values: Token): void {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(values))
}
// 获取token
export function getToken(): Token {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || '{}')
}

//删除token
export function removeToken(): void {
  localStorage.removeItem(TOKEN_KEY)
}

// 判断有无token  因为 永远都不是false 要么就是 有 要么就是{}
export function hasToken(): boolean {
  return !!getToken().token
}
// 存储 用户频道列表
export function setChannel(channel: Channel[]) {
  localStorage.setItem(GEEK_CHANNEL, JSON.stringify(channel))
}
//获取 用户频道列表
export function getChannel() {
  return JSON.parse(localStorage.getItem(GEEK_CHANNEL) || '[]')
}
// 把搜索的历史记录存储的本地存储
export function setHistory(history: string[]) {
  localStorage.setItem(GEEK_HISTORY, JSON.stringify(history))
}
// 从本地存储获取搜索的历史记录
export function getHistory() {
  return JSON.parse(localStorage.getItem(GEEK_HISTORY) || '[]')
}
